//
//  ZFBaseViewController1.m
//  HappilyLoan
//
//  Created by zainishenbian on 2017/3/24.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "ZFBaseViewController1.h"
#import "ZFBaseInfoViewController2.h"
#import "ZFCityPickerView.h"
#import "UIViewController+BackButtonHandler.h"
#import "ZFHeader.h"

@interface ZFBaseViewController1 ()<UITextViewDelegate>
{
    NSString *startTime;//进入页面的时间
    
    UIView *pickerViewBgView;
    UIView *locationView;
    
    NSString *_locationStr;//地理地址
    NSString *_geocoding;//地理编码
}
@property (nonatomic,strong) ZFCityPickerView *cityPickerView;
@property (nonatomic,strong) AnimatingView *animation;
@property (nonatomic,strong) AlertBoxView *alertV;
@end

@implementation ZFBaseViewController1

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"baseView"];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"baseView"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"基本信息";
    self.view.backgroundColor = BGCOLOR;

    [self initData];
}

- (void)action_DailyNotification:(NSNotification *)no
{
    
}
//初始化数据
- (void)initData
{
    _detailAdress.text = @"请输入您的详细地址";
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"toLocationString"]) {
        
        [_chooseCityButton setTitle:[[NSUserDefaults standardUserDefaults] objectForKey:@"toLocationString"] forState:UIControlStateNormal];
        _geocoding = [[NSUserDefaults standardUserDefaults] objectForKey:@"adcodeString"];
    }
    startTime = [ZFPulicAPI getCurrentTimestamp];
    
    _animation = [[AnimatingView alloc] init];
    
    _alertV = [[AlertBoxView alloc] init];
    [self.view addSubview:_alertV];
    
    _cityPickerView = [[ZFCityPickerView alloc] init];
    _cityPickerView.dataSource = @[@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1"];
}
#pragma mark ———— 对将要提交的信息进行判断
- (BOOL)isJudgeSubmitInfo
{
    if ([ZFPulicAPI replaceBlank:_name.text].length > 0 && [ZFPulicAPI replaceBlank:_idcard.text].length > 0 && ![_chooseCityButton.titleLabel.text isEqualToString:@"请选择城市"] && ![_detailAdress.text isEqualToString:@"请输入您的详细地址"] && [ZFPulicAPI replaceBlank:_detailAdress.text].length > 0) {
        
        if ([ZFPulicAPI verifyIDcard:[ZFPulicAPI replaceBlank:_idcard.text]]) {
            
            return YES;
        }
        else
        {
            [_alertV appearAndDisappearAlertWithString:@"身份证格式不正确" ];
        }
    }
    else
    {
        [_alertV appearAndDisappearAlertWithString:@"信息不完善" ];
    }
    
    return NO;
}
#pragma mark ———— textview delegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"请输入您的详细地址"])
    {
        textView.text = @"";
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length < 1)
    {
        textView.text = @"请输入您的详细地址";
    }
}

#pragma mark ———— view的触摸手势事件
- (void)pickerViewBGClick:(UITapGestureRecognizer *)tap
{
    pickerViewBgView.hidden = YES;
    locationView.hidden = YES;
}
#pragma mark ———— button的点击事件
//选择地址
- (IBAction)chooseCity:(id)sender {
    
    UIButton *button = (UIButton *)sender;
    _cityPickerView.valueDidSelect = ^(NSString *locationStr, NSString *geocoding) {
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _locationStr = locationStr;
        _geocoding = geocoding;
        [button setTitle:locationStr forState:UIControlStateNormal];
    };
    [_cityPickerView show];
}
//下一步
- (IBAction)nextStep:(id)sender {
    
    if (![self isJudgeSubmitInfo]) {
        
        return;
    }

    ZFUserBaseInfoModal *userBaseInfo = [ZFUserBaseInfoModal new];
    userBaseInfo.true_name = [ZFPulicAPI replaceBlank:_name.text];
    userBaseInfo.idcard_no = [ZFPulicAPI replaceBlank:_idcard.text];
    userBaseInfo.ad_path = _chooseCityButton.titleLabel.text;
    userBaseInfo.ad_code = _geocoding;
    userBaseInfo.detail_address = [ZFPulicAPI replaceBlank:_detailAdress.text];
    userBaseInfo.startTime = startTime;
    
    [_animation startTheSmallAnimation];
    [ZFApplyInfoHttpRequest requestSubmitUserBaseInfo:userBaseInfo sucessBlock:^(NSDictionary *dict) {
       
        [_animation stopTheSmallAnimation];
        
        if ([dict[@"is_alert"] integerValue] == 1) {
            
            [_alertV appearAndDisappearAlertWithString:dict[@"msg"]];
        }
        if ([dict[@"code"] integerValue] == 1) {
    
            ZFBaseInfoViewController2 *baseVC2 = [ZFBaseInfoViewController2 new];
            baseVC2.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:baseVC2 animated:YES];
            [[NSUserDefaults standardUserDefaults] setObject:[ZFPulicAPI replaceBlank:_name.text] forKey:@"true_name"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    } failureBlock:^(NSInteger failSign) {
        [_animation stopTheSmallAnimation];
        [_alertV appearAndDisappearAlertWithString:@"网络连接失败"];
    }];
}
#pragma mark ———— 返回
- (BOOL)navigationShouldPopOnBackButton
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (delegate.isFirstEnterApp == YES) {
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        delegate.isFirstEnterApp = NO;
        return NO;
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
